package com.example.demotext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }


    private void getNetTime() {
        URL url = null;//取得资源对象
        try {
            url = new URL("http://www.baidu.com");
//            url = new URL("http://www.ntsc.ac.cn");//中国科学院国家授时中心
            //url = new URL("http://www.bjtime.cn");
            URLConnection uc = url.openConnection();//生成连接对象
            uc.connect(); //发出连接
            long ld = uc.getDate(); //取得网站日期时间
            Date date = new Date(ld);
            SimpleDateFormat dff = new SimpleDateFormat("HH");
            dff.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
            final String ee = dff.format(date);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    Log.d("lyf", ee);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void oT(View view) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                getNetTime();

            }
        }).start();

    }
}
